/**
 * 核心数据模型定义
 * 与后端接口严格对齐的类型定义
 */

// ============= 基础类型定义 =============

/**
 * 统一的ID类型 - 与后端ObjectId字符串格式对齐
 */
export type EntityId = string

/**
 * 时间戳类型 - ISO 8601格式字符串
 */
export type Timestamp = string

/**
 * 分页参数接口
 */
export interface PaginationParams {
  page?: number
  size?: number
  skip?: number
  limit?: number
}

/**
 * 分页响应接口
 */
export interface PaginationMeta {
  page: number
  size: number
  total: number
  pages: number
  has_next: boolean
  has_prev: boolean
}

/**
 * 统一API响应格式
 */
export interface ApiResponse<T = any> {
  success: boolean
  code: number
  message: string
  data: T
  pagination?: PaginationMeta
  timestamp: Timestamp
  request_id: string
}

/**
 * 分页数据响应
 */
export interface PaginatedResponse<T> extends ApiResponse<T[]> {
  pagination: PaginationMeta
}

// ============= 实体基础接口 =============

/**
 * 基础实体接口 - 所有实体的公共字段
 */
export interface BaseEntity {
  id: EntityId
  created_at: Timestamp
  updated_at: Timestamp
  created_by: EntityId
  updated_by?: EntityId
}

/**
 * 可软删除实体接口
 */
export interface SoftDeletableEntity extends BaseEntity {
  is_deleted: boolean
  deleted_at?: Timestamp
  deleted_by?: EntityId
}

/**
 * 可归档实体接口
 */
export interface ArchivableEntity extends BaseEntity {
  is_archived: boolean
  archived_at?: Timestamp
  archived_by?: EntityId
}

/**
 * 有状态实体接口
 */
export interface StatefulEntity extends BaseEntity {
  status: string
  status_updated_at?: Timestamp
  status_updated_by?: EntityId
}

// ============= 枚举定义 =============

/**
 * 模块类型枚举 - 与后端严格对齐
 */
export enum ModuleType {
  PROJECT = 'project',
  REQUIREMENT = 'requirement',
  TASK = 'task',
  DEFECT = 'defect',
  VERSION = 'version',
  DOCUMENT = 'document',
  USER = 'user',
  COMMENT = 'comment',
  NOTIFICATION = 'notification',
  PROJECT_PRIORITY = 'project_priority',
  REQUIREMENT_PRIORITY = 'requirement_priority',
  TASK_PRIORITY = 'task_priority',
  DEFECT_PRIORITY = 'defect_priority',
  TEST_CASE = 'test_case',
  TEST_PLAN = 'test_plan',
  APPROVAL = 'approval'
}

/**
 * 优先级枚举 - 通用优先级定义
 */
export enum Priority {
  LOW = 'low',
  MEDIUM = 'medium',
  HIGH = 'high',
  URGENT = 'urgent',
  CRITICAL = 'critical'
}

/**
 * 状态类型枚举
 */
export enum StateType {
  INITIAL = 'initial',    // 初始状态
  ACTIVE = 'active',      // 活跃状态
  FINAL = 'final',        // 最终状态
  ARCHIVED = 'archived'   // 归档状态
}

// ============= 验证规则接口 =============

/**
 * 字段验证规则
 */
export interface ValidationRule {
  required?: boolean
  min_length?: number
  max_length?: number
  pattern?: string
  custom_validator?: (value: any) => boolean | string
}

/**
 * 实体验证规则集合
 */
export interface EntityValidationRules {
  [fieldName: string]: ValidationRule
}

// ============= 默认值配置 =============

/**
 * 默认值配置接口
 */
export interface DefaultValueConfig<T = any> {
  [fieldName: string]: T | (() => T)
}

/**
 * 实体默认值工厂
 */
export interface EntityDefaults<T extends BaseEntity> {
  (): Partial<T>
}

// ============= 错误处理 =============

/**
 * 业务错误类型
 */
export enum ErrorType {
  VALIDATION_ERROR = 'validation_error',
  NOT_FOUND = 'not_found',
  PERMISSION_DENIED = 'permission_denied',
  CONFLICT = 'conflict',
  NETWORK_ERROR = 'network_error',
  SERVER_ERROR = 'server_error',
  TIMEOUT = 'timeout'
}

/**
 * 业务错误接口
 */
export interface BusinessError {
  type: ErrorType
  code: string
  message: string
  details?: Record<string, any>
  field?: string
  timestamp: Timestamp
}

// ============= 操作日志 =============

/**
 * 操作类型枚举
 */
export enum OperationType {
  CREATE = 'create',
  UPDATE = 'update',
  DELETE = 'delete',
  ARCHIVE = 'archive',
  RESTORE = 'restore',
  STATUS_CHANGE = 'status_change'
}

/**
 * 操作日志接口
 */
export interface OperationLog {
  id: EntityId
  entity_type: string
  entity_id: EntityId
  operation_type: OperationType
  old_values?: Record<string, any>
  new_values?: Record<string, any>
  user_id: EntityId
  timestamp: Timestamp
  ip_address?: string
  user_agent?: string
}

// ============= 缓存配置 =============

/**
 * 缓存策略枚举
 */
export enum CacheStrategy {
  NO_CACHE = 'no_cache',
  MEMORY_ONLY = 'memory_only',
  LOCAL_STORAGE = 'local_storage',
  SESSION_STORAGE = 'session_storage',
  INDEXED_DB = 'indexed_db'
}

/**
 * 缓存配置接口
 */
export interface CacheConfig {
  strategy: CacheStrategy
  ttl: number // 缓存时间（毫秒）
  max_size?: number // 最大缓存条目数
  auto_refresh?: boolean // 是否自动刷新
  refresh_interval?: number // 自动刷新间隔（毫秒）
}

// ============= 同步配置 =============

/**
 * 同步策略枚举
 */
export enum SyncStrategy {
  MANUAL = 'manual',
  POLLING = 'polling',
  WEBSOCKET = 'websocket',
  SERVER_SENT_EVENTS = 'sse'
}

/**
 * 同步配置接口
 */
export interface SyncConfig {
  strategy: SyncStrategy
  interval?: number // 轮询间隔（毫秒）
  retry_count?: number // 重试次数
  retry_delay?: number // 重试延迟（毫秒）
  auto_reconnect?: boolean // 是否自动重连
}